import {Component, OnInit, ElementRef, OnDestroy, TemplateRef} from '@angular/core';
import {Router} from '@angular/router';
import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
import {NormalService} from "../../../apiServices/normal.service";
import {Subscription} from 'rxjs/Subscription';

@Component({
    selector: 'pay-password',
    templateUrl: './pay-password.template.html'
})

export class PayPasswordComponent implements OnInit, OnDestroy{
    public language: string = 'en';
    public payPasswordModel: any = {
        id: '',
        loginPassword: '',
        payPassword:'',
        repeatPassword: '',
        oldpass: '',
        newpass: '',
        repass: '',
        email: ''
    };
    public user: any;

    passValid: boolean = true;
    ispassValid: boolean = true;
   isSetPayPassword: boolean = false;

    modalRef: BsModalRef;
    modalRef2: BsModalRef;
    public userSubscription: Subscription;
    updatePayPassSucc: any = {en: 'Reset paypassword successful', zh: '重置支付密码成功！'};
    createPayPassSucc: any = {en: 'Set paypassword successful', zh: '支付密码设置成功！'};

    canGetEmail: boolean = true;
    getEmailName: string = 'submit';
    sendEmailError: boolean = false;

    constructor(private modalService: BsModalService, 
        private normalService: NormalService,
        private router: Router
    ) {
        this.language = normalService.lang;
            if(normalService.isLogin()){
                normalService.getuser();
            }

            this.userSubscription = normalService.user$.subscribe(user => {
                this.payPasswordModel.id = user._id;
                this.user = user;
                console.log('isSetpaypass',user, !!user.pay_set);
                this.isSetPayPassword = !!user.pay_set?true:false;
            })

            if(this.normalService.lang == 'en'){
                this.getEmailName = 'submit';
            }else if(this.normalService.lang == 'zh'){
                this.getEmailName = '提交';
            }

        }
    
    openModal(template: TemplateRef<any>) {
        this.modalRef = this.modalService.show(template);
    }
    openModal2(template: TemplateRef<any>) {
        this.modalRef2 = this.modalService.show(template);
      }
    

    isSame(){
        if (this.payPasswordModel.payPassword != this.payPasswordModel.repeatPassword){
            this.passValid = false;
        } else{
            this.passValid = true;
        }
    }

    isPayPassSame(){
        if (this.payPasswordModel.newpass != this.payPasswordModel.repass){
            this.ispassValid = false;
        } else{
            this.ispassValid = true;
        }
    }

    forgetPayPassword(id: string, email: string): void{
        this.canGetEmail = false;
        if(this.normalService.lang == 'en'){
            this.getEmailName = 'sending...';
        }else if(this.normalService.lang == 'zh'){
            this.getEmailName = '发送中...';
        }
        this.normalService.forgetPayPassword(id, email).then((result) => {
            if(result && result.code && result.code ===200){
                alert(this.normalService.getSucc(result.message));
                    this.sendEmailError = false;
                    this.canGetEmail = false;
                    let count = 120;
                    this.getEmailName = count.toString();
                    let id = setInterval(() => {
                        --count;
                        if(count === 0){
                            clearInterval(id);
                            if(this.normalService.lang == 'en'){
                                this.getEmailName = 'submit';
                            }else if(this.normalService.lang == 'zh'){
                                this.getEmailName = '提交';
                            }
                            this.canGetEmail = true;
                        }else{
                            this.getEmailName = count.toString();
                        }
                    },1000);
                }else{
                    this.sendEmailError = false;
                    this.canGetEmail = true;
                    if(this.normalService.lang == 'en'){
                        this.getEmailName = 'submit';
                    }else if(this.normalService.lang == 'zh'){
                        this.getEmailName = '提交';
                    }
                }
        }).catch((error) =>{
            this.sendEmailError = false;
            this.canGetEmail = true;
            if(this.normalService.lang == 'en'){
                this.getEmailName = 'submit';
            }else if(this.normalService.lang == 'zh'){
                this.getEmailName = '提交';
            }
            alert(this.normalService.getErrors(error));
        });
    }

    createPayPassword(loginPass: string, payPass: string): void{
        this.normalService.createPayPassword(loginPass, payPass).then((result) => {
            if(result.message){
                alert(this.normalService.getSucc(result.message));
                
                //alert(result.success);
            }else{
                if(this.normalService.lang == 'en'){
                    alert(this.createPayPassSucc.en);
                }
                if(this.normalService.lang == 'zh'){
                    alert(this.createPayPassSucc.zh);
                } 
                
                //alert('Reset paypassword successful');
            }
            this.router.navigate(['/account']);
        }).catch((error) =>{
            alert(this.normalService.getErrors(error));
        })
    }

    updatePayPassword(id: string, oldpass: string, newpass): void{
        this.normalService.updatePayPassword(id, oldpass, newpass).then((result) => {
            if(result.message){
                alert(this.normalService.getSucc(result.message));
                
                //alert(result.success);
            }else{
                if(this.normalService.lang == 'en'){
                    alert(this.updatePayPassSucc.en);
                }
                if(this.normalService.lang == 'zh'){
                    alert(this.updatePayPassSucc.zh);
                } 
                
                //alert('Reset paypassword successful');
            }
        }).catch((error) =>{
            alert(this.normalService.getErrors(error));
        })
    }


    ngOnDestroy(): void{}
    ngOnInit(): void{}
}